//管理器
const roleEntity = require("./role.js")
const oneDayTime = 3600000
var DAYPLAYER_NUM = 3000 			//生成玩家数量
var CREATE_TIME	= 10 * oneDayTime 	//玩家生成间隔
var ONE_TIME = Math.round(CREATE_TIME / DAYPLAYER_NUM)  //单玩家平均时间
var type = "chuhai_shanhai"
var http = require("http")
console.log("每日生成玩家数量",DAYPLAYER_NUM)
console.log("生成时间间隔:"+ONE_TIME+"毫秒")

var model = function() {}
//初始化
model.init = function() {
	this.playerId = 1
	this.players = {}
	this.createTime = 0
	setInterval(this.update.bind(this),100)
}
//生成角色
model.createPlayer = function() {
	http.get("http://112.74.171.233/addLoadCount?type="+type,function(res) {})
	if(Math.random() < 0.3)
		http.get("http://112.74.171.233/addLoadCount?type="+type,function(res) {})
	console.log("开始生成角色",this.playerId,"待投放数量",DAYPLAYER_NUM)
	this.players[this.playerId] = new roleEntity(this,this.playerId)
	this.playerId++
	console.log("当前活跃数量:",Object.keys(this.players).length)
}
//销毁角色
model.destoryPlayer = function(id) {
	console.log("角色销毁",id)
	delete this.players[id]
}
//定时器
model.update = function() {
	var cueTime = Date.now()
	//玩家刷新
	for(var i in this.players)
		this.players[i].update()
	//生成角色判断
	if(DAYPLAYER_NUM > 0 && Date.now() > this.createTime){
		DAYPLAYER_NUM--
		if(DAYPLAYER_NUM <= 0)
			console.log("角色投放完成!!!!")
		var nextTime = ONE_TIME * (0.5 + Math.random())
		this.createTime = Date.now() + nextTime
		model.createPlayer()
	}
}
//初始化，运行定时器
model.init()
